class safe {
    Chars;
    constructor(str) {
        let result = "";
        for (let i = 0; i < str.length; i++) {
            if (result.indexOf(str[i]) === -1) {
                result += str[i];
            }
        }
        this.Chars = result;
    }
    /**
     * @description: 将字符串转换为UTF-8编码的字节数组
     * @param {String} str 要转换的字符串
     * @return {Array} 转换后的字节数组
     */
    stringToUtf8Bytes(str) {
        let utf8 = [];
        for (let i = 0; i < str.length; i++) {
            let charCode = str.charCodeAt(i);
            if (charCode < 128) {
                utf8.push(charCode);
            } else if (charCode < 2048) {
                utf8.push((charCode >> 6) | 192);
                utf8.push((charCode & 63) | 128);
            } else {
                utf8.push((charCode >> 12) | 224);
                utf8.push(((charCode >> 6) & 63) | 128);
                utf8.push((charCode & 63) | 128);
            }
        }
        return utf8;
    }

    /**
     * @description: 将 UTF-8 编码的字节数组转换回字符串
     * @param {Array} bytes 要转换的字节数组
     * @return {String} 转换后的字符串
     */
    utf8BytesToString(bytes) {
        let str = '';
        let i = 0;
        while (i < bytes.length) {
            let charCode;
            if (bytes[i] < 128) {
                charCode = bytes[i];
                i++;
            } else if (bytes[i] < 224) {
                charCode = ((bytes[i] & 31) << 6) | (bytes[i + 1] & 63);
                i += 2;
            } else {
                charCode = ((bytes[i] & 15) << 12) | ((bytes[i + 1] & 63) << 6) | (bytes[i + 2] & 63);
                i += 3;
            }
            str += String.fromCharCode(charCode);
        }
        return str;
    }

    /**
     * @description: 将数字转为字符串
     * @param {Number} number
     * @return {String}
     */
    convertNumberToText(number) {
        let result = "";

        while (number > 0) {
            let remainder = number % this.Chars.length;
            result = this.Chars[remainder] + result;
            number = Math.floor(number / this.Chars.length);
        }

        return result;
    }

    /**
     * @description: 将文本转为数字
     * @param {Text} text 
     * @return {Number}
     */
    convertTextToNumber(text) {
        let number = 0;
        let power = 0;

        for (let i = text.length - 1; i >= 0; i--) {
            const charIndex = this.Chars.indexOf(text[i]);
            number += charIndex * Math.pow(this.Chars.length, power);
            power++;
        }
        return number;
    }

    /**
     * @description: 解密文本
     * @param {String} str 待解密的文本
     * @return {String}
     */
    decrypt(str) {
        let b = [],
            v = 0,
            s = "",
            NumArray = [];
        for (let i = 0; i < str.length; i++) {
            const ele = str[i];
            // console.log(v);
            if (v) {
                v--;
                s = s + ele;
            } else {
                v = parseInt(this.convertTextToNumber(str[i]));
                b.push(s);
                s = "";
            }
        }
        b.push(s)
        b.splice(0, 1)
        b.forEach(item => {
            NumArray.push(this.convertTextToNumber(item))
        })
        return this.utf8BytesToString(NumArray)
    }
}